<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ page import="com.example.bean.Resident, com.example.dao.ResidentDAO, java.text.SimpleDateFormat" %>
<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>居民信息修改 - 社区管理系统</title>
    <!-- 引入Bootstrap美化界面 -->
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css" rel="stylesheet">
    <!-- 引入Bootstrap图标 -->
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap-icons@1.10.0/font/bootstrap-icons.css">
    <!-- 引入Flatpickr日期选择器 -->
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/flatpickr/dist/flatpickr.min.css">
    <style>
        :root {
            --primary-color: #3498db;
            --secondary-color: #2c3e50;
            --card-shadow: 0 8px 20px rgba(0,0,0,0.12);
            --form-radius: 10px;
        }
        body {
            background: linear-gradient(135deg, #f5f7fa 0%, #e4e7eb 100%);
            min-height: 100vh;
            font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
            padding: 2rem 0;
        }
        .registration-card {
            background: white;
            border-radius: 15px;
            box-shadow: var(--card-shadow);
            overflow: hidden;
            transition: transform 0.3s ease;
            max-width: 800px;
            margin: 0 auto;
        }
        .registration-card:hover {
            transform: translateY(-5px);
        }
        .registration-header {
            background: linear-gradient(135deg, var(--primary-color), var(--secondary-color));
            color: white;
            padding: 30px 20px;
            text-align: center;
        }
        .form-section {
            margin-bottom: 25px;
            padding: 20px;
            background: #f8fafc;
            border-radius: var(--form-radius);
            border-left: 4px solid var(--primary-color);
        }
        .form-icon {
            position: absolute;
            left: 15px;
            top: 50%;
            transform: translateY(-50%);
            color: var(--primary-color);
            z-index: 5;
        }
        .form-control, .form-select {
            padding-left: 45px;
            height: 50px;
            border-radius: var(--form-radius);
            border: 1px solid #e1e5eb;
            transition: all 0.3s;
        }
        .form-control:focus, .form-select:focus {
            border-color: var(--primary-color);
            box-shadow: 0 0 0 0.25rem rgba(52, 152, 219, 0.25);
        }
        .btn-update {
            background: linear-gradient(135deg, var(--primary-color), var(--secondary-color));
            border: none;
            height: 50px;
            font-size: 1.1rem;
            font-weight: 600;
            border-radius: var(--form-radius);
            transition: all 0.3s;
        }
        .btn-update:hover {
            transform: translateY(-2px);
            box-shadow: 0 6px 12px rgba(52, 152, 219, 0.3);
        }
        .error-message {
            background-color: #f8d7da;
            color: #721c24;
            padding: 15px;
            border-radius: var(--form-radius);
            margin-bottom: 20px;
            border-left: 4px solid #dc3545;
        }
        .section-title {
            color: var(--secondary-color);
            border-bottom: 2px dashed #e1e5eb;
            padding-bottom: 10px;
            margin-bottom: 20px;
            display: flex;
            align-items: center;
            gap: 10px;
        }
        .flatpickr-input {
            background-color: white;
        }
    </style>
</head>
<body>
<%
    // 设置响应编码防止乱码
    response.setContentType("text/html;charset=UTF-8");
    response.setCharacterEncoding("UTF-8");

    String idParam = request.getParameter("id");
    int id = 0;
    if (idParam != null && !idParam.isEmpty()) {
        try {
            id = Integer.parseInt(idParam);
        } catch (NumberFormatException e) {
%>
<div class="container">
    <div class="error-container text-center">
        <i class="bi bi-exclamation-triangle-fill text-danger" style="font-size: 3rem;"></i>
        <h2 class="mt-3">操作失败</h2>
        <p class="lead">无效的居民 ID，请检查输入</p>
        <a href="javascript:history.back()" class="btn btn-primary mt-3">
            <i class="bi bi-arrow-left"></i> 返回上一页
        </a>
    </div>
</div>
<%
        return;
    }
} else {
%>
<div class="container">
    <div class="error-container text-center">
        <i class="bi bi-exclamation-circle-fill text-warning" style="font-size: 3rem;"></i>
        <h2 class="mt-3">请求参数缺失</h2>
        <p class="lead">未提供居民 ID，请检查请求</p>
        <a href="javascript:history.back()" class="btn btn-primary mt-3">
            <i class="bi bi-arrow-left"></i> 返回上一页
        </a>
    </div>
</div>
<%
        return;
    }

    ResidentDAO residentDAO = new ResidentDAO();
    Resident resident = residentDAO.getResidentById(id);
    if (resident == null) {
%>
<div class="container">
    <div class="error-container text-center">
        <i class="bi bi-person-x-fill text-danger" style="font-size: 3rem;"></i>
        <h2 class="mt-3">信息未找到</h2>
        <p class="lead">未找到对应的居民信息，请检查 ID</p>
        <a href="javascript:history.back()" class="btn btn-primary mt-3">
            <i class="bi bi-arrow-left"></i> 返回上一页
        </a>
    </div>
</div>
<%
        return;
    }

    // 格式化日期
    String formattedDate = "";
    if (resident.getBirthDate() != null) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        formattedDate = sdf.format(resident.getBirthDate());
    }
%>

<div class="container">
    <div class="registration-card">
        <div class="registration-header">
            <h2 class="mb-3"><i class="bi bi-person-badge"></i> 居民信息修改</h2>
            <p class="mb-0">更新居民资料信息</p>
        </div>

        <div class="registration-form p-4">
            <% if (request.getAttribute("error") != null) { %>
            <div class="error-message mb-4">
                <i class="bi bi-exclamation-circle me-2"></i> <%= request.getAttribute("error") %>
            </div>
            <% } %>

            <form action="resident" method="post">
                <input type="hidden" name="action" value="update">
                <input type="hidden" name="id" value="<%= resident.getId() %>">

                <!-- 基本信息区域 -->
                <div class="form-section mb-4">
                    <h4 class="section-title">
                        <i class="bi bi-person"></i> 基本信息
                    </h4>

                    <div class="row">
                        <div class="col-md-6 mb-4 position-relative">
                            <label for="name" class="form-label fw-bold">姓名</label>
                            <i class="bi bi-person-badge form-icon"></i>
                            <input type="text" id="name" name="name" class="form-control"
                                   placeholder="请输入居民姓名" required
                                   value="<%= resident.getName() != null ? resident.getName() : "" %>">
                        </div>

                        <div class="col-md-6 mb-4 position-relative">
                            <label for="gender" class="form-label fw-bold">性别</label>
                            <i class="bi bi-gender-ambiguous form-icon"></i>
                            <select id="gender" name="gender" class="form-select" required>
                                <option value="">请选择性别</option>
                                <option value="男" <%= "男".equals(resident.getGender()) ? "selected" : "" %>>男</option>
                                <option value="女" <%= "女".equals(resident.getGender()) ? "selected" : "" %>>女</option>
                            </select>
                        </div>
                    </div>

                    <div class="mb-4 position-relative">
                        <label for="birthDate" class="form-label fw-bold">出生日期</label>
                        <i class="bi bi-calendar form-icon"></i>
                        <input type="text" id="birthDate" name="birthDate" class="form-control flatpickr-input"
                               placeholder="选择出生日期" required
                               value="<%= formattedDate %>">
                    </div>
                </div>

                <!-- 联系信息区域 -->
                <div class="form-section mb-4">
                    <h4 class="section-title">
                        <i class="bi bi-geo-alt"></i> 联系信息
                    </h4>

                    <div class="mb-4 position-relative">
                        <label for="address" class="form-label fw-bold">详细地址</label>
                        <i class="bi bi-house form-icon"></i>
                        <input type="text" id="address" name="address" class="form-control"
                               placeholder="请输入详细居住地址" required
                               value="<%= resident.getAddress() != null ? resident.getAddress() : "" %>">
                    </div>
                </div>

                <!-- 健康信息区域 -->
                <div class="form-section mb-4">
                    <h4 class="section-title">
                        <i class="bi bi-heart-pulse"></i> 健康信息
                    </h4>

                    <div class="mb-4 position-relative">
                        <label for="healthStatus" class="form-label fw-bold">健康状态</label>
                        <i class="bi bi-clipboard-pulse form-icon"></i>
                        <select id="healthStatus" name="healthStatus" class="form-select" required>
                            <option value="">请选择健康状态</option>
                            <option value="健康" <%= "健康".equals(resident.getHealthStatus()) ? "selected" : "" %>>健康</option>
                            <option value="良好" <%= "良好".equals(resident.getHealthStatus()) ? "selected" : "" %>>良好</option>
                            <option value="一般" <%= "一般".equals(resident.getHealthStatus()) ? "selected" : "" %>>一般</option>
                            <option value="需关注" <%= "需关注".equals(resident.getHealthStatus()) ? "selected" : "" %>>需关注</option>
                            <option value="慢性病" <%= "慢性病".equals(resident.getHealthStatus()) ? "selected" : "" %>>慢性病管理</option>
                        </select>
                    </div>
                </div>

                <div class="d-grid gap-2 mt-4">
                    <button type="submit" class="btn btn-update text-white">
                        <i class="bi bi-check-circle me-2"></i> 更新居民信息
                    </button>
                    <a href="javascript:history.back()" class="btn btn-outline-secondary">
                        <i class="bi bi-x-circle me-2"></i> 取消返回
                    </a>
                </div>
            </form>
        </div>
    </div>

    <div class="mt-4 text-center text-muted">
        <small>© 2025 社区管理系统 | 居民服务平台</small>
    </div>
</div>

<!-- 引入Bootstrap JS -->
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/js/bootstrap.bundle.min.js"></script>
<!-- 引入Flatpickr日期选择器 -->
<script src="https://cdn.jsdelivr.net/npm/flatpickr"></script>
<script src="https://cdn.jsdelivr.net/npm/flatpickr/dist/l10n/zh.js"></script>
<script>
    // 初始化日期选择器
    flatpickr("#birthDate", {
        dateFormat: "Y-m-d",
        locale: "zh",
        maxDate: "today",
        allowInput: true
    });
</script>
</body>
</html>